Ext.define('yxt.view.LoginWin', {
  	  extend: 'Ext.window.Window',
  	  alias: 'widget.loginWin',
	  title: '书房',
	  closable: false,
	  width: 300,
	  height: 200,
	  layout: 'fit',
	  initComponent: function() {
		  var me = this;
		  me.items = [{
				xtype: 'form',
				url: 'user/login.do',
				layout: 'anchor',
			    border: 0,
		
			    defaults: {
			    	xtype: 'textfield',
			    	labelAlign: 'right',
			    	labelStyle: 'font-size:15px',
			    	labelWidth: 60,
			    	width: 100,
			    	height: 30,
			    	anchor: '100%',
			    	margin: '15,0,0,0'
			    },
			    items: [{
			        fieldLabel: '用户名',
			        name: 'user_id',
			        allowBlank: false
			    },{
			        fieldLabel: '密码',
			        name: 'password',
			        inputType: 'password',
			        enableKeyEvents: true,
			        listeners: {
			        	keypress: function(scope, e) {
							if(e.keyCode == e.ENTER || e.keyCode == 10) {
								me.login();
							}
			        	}
			        }
			    },
			    {
			    	xtype: 'panel',
			    	layout: 'hbox',
			    	border: 0,
			    	
			    	margin: '20 0 0 50',
			    	defaults: {
			    		margin: '0 18 0 18',
			    		height: 30,
			    		flex: 1
			    	},
			    	items: [{
				    	xtype: 'button',
				        text: '<font size=2>登录</font>',
				        formBind: true, //only enabled once the form is valid
				        handler: function() {
				        	me.login();
				        }
				    },{
				    	xtype: 'button',
				        text: '<font size=2>注册</font>',
				        handler: function() {
				            //Open register page
				        	me.close();
				        	var registerWin = Ext.create('yxt.view.RegisterWin');
				        	registerWin.show();
				        }
				    }]
			    }],
			    renderTo: Ext.getBody()
			}];
		  me.callParent(arguments);
	  },
	  login: function() {
		  var me = this;
          var form = me.down('form').getForm();
          if (form.isValid()) {
              form.submit({
                  success: function(form, action) {
                  	me.close();
                  	var chatWin = Ext.create('yxt.view.ChatWin', {
                  		name: action.result.name,
                  		user_id: action.result.user_id
                  	});
                  	chatWin.chatHis.add(action.result.chatHisList);
                  	chatWin.show();
                  	chatWin.sendRequest(null);
                  	chatWin.getOnline(false);
                  },
                  failure: function(form, action) {
                      Ext.Msg.alert('密码错误', '没有该用户或密码错误');
                  }
              });
          }
	  }
});